<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="船舶ID" prop="shipId">
        <el-input
          v-model="queryParams.shipId"
          placeholder="请输入船舶ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="船舶注册号" prop="shipRegNo">
        <el-input
          v-model="queryParams.shipRegNo"
          placeholder="请输入船舶注册号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="初次注册号" prop="shipFirstRegNo">
        <el-input
          v-model="queryParams.shipFirstRegNo"
          placeholder="请输入初次注册号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验号" prop="shipInspectNo">
        <el-input
          v-model="queryParams.shipInspectNo"
          placeholder="请输入检验号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="MMSI号" prop="MMSI">
        <el-input
          v-model="queryParams.MMSI"
          placeholder="请输入MMSI号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册港口代码" prop="regportCode">
        <el-input
          v-model="queryParams.regportCode"
          placeholder="请输入注册港口代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型代码" prop="shipTypeCode">
        <el-input
          v-model="queryParams.shipTypeCode"
          placeholder="请输入类型代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总吨位" prop="shipGrossTon">
        <el-input
          v-model="queryParams.shipGrossTon"
          placeholder="请输入总吨位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="净吨位" prop="shipNetTon">
        <el-input
          v-model="queryParams.shipNetTon"
          placeholder="请输入净吨位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="载重吨" prop="shipDwt">
        <el-input
          v-model="queryParams.shipDwt"
          placeholder="请输入载重吨"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="长度" prop="shipLength">
        <el-input
          v-model="queryParams.shipLength"
          placeholder="请输入长度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宽度" prop="shipBreadth">
        <el-input
          v-model="queryParams.shipBreadth"
          placeholder="请输入宽度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="深度" prop="shipDepth">
        <el-input
          v-model="queryParams.shipDepth"
          placeholder="请输入深度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="航线代码" prop="lineCode">
        <el-input
          v-model="queryParams.lineCode"
          placeholder="请输入航线代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域代码" prop="areaCode">
        <el-input
          v-model="queryParams.areaCode"
          placeholder="请输入区域代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建造日期" prop="constructionDate">
        <el-date-picker clearable
          v-model="queryParams.constructionDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择建造日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dataSys:ships:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dataSys:ships:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dataSys:ships:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dataSys:ships:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shipsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="船舶ID" align="center" prop="shipId" />
      <el-table-column label="船舶注册号" align="center" prop="shipRegNo" />
      <el-table-column label="初次注册号" align="center" prop="shipFirstRegNo" />
      <el-table-column label="检验号" align="center" prop="shipInspectNo" />
      <el-table-column label="船名" align="center" prop="shipNameCn" />
      <el-table-column label="MMSI号" align="center" prop="mmsi" />
      <el-table-column label="注册港口代码" align="center" prop="regportCode" />
      <el-table-column label="区域类型" align="center" prop="shipRegionType" />
      <el-table-column label="类型代码" align="center" prop="shipTypeCode" />
      <el-table-column label="总吨位" align="center" prop="shipGrossTon" />
      <el-table-column label="净吨位" align="center" prop="shipNetTon" />
      <el-table-column label="载重吨" align="center" prop="shipDwt" />
      <el-table-column label="长度" align="center" prop="shipLength" />
      <el-table-column label="宽度" align="center" prop="shipBreadth" />
      <el-table-column label="深度" align="center" prop="shipDepth" />
      <el-table-column label="航线代码" align="center" prop="lineCode" />
      <el-table-column label="区域代码" align="center" prop="areaCode" />
      <el-table-column label="建造日期" align="center" prop="constructionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.constructionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dataSys:ships:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dataSys:ships:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改船舶信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="船舶ID" prop="shipId">
          <el-input v-model="form.shipId" placeholder="请输入船舶ID" />
        </el-form-item>
        <el-form-item label="船舶注册号" prop="shipRegNo">
          <el-input v-model="form.shipRegNo" placeholder="请输入船舶注册号" />
        </el-form-item>
        <el-form-item label="初次注册号" prop="shipFirstRegNo">
          <el-input v-model="form.shipFirstRegNo" placeholder="请输入初次注册号" />
        </el-form-item>
        <el-form-item label="检验号" prop="shipInspectNo">
          <el-input v-model="form.shipInspectNo" placeholder="请输入检验号" />
        </el-form-item>
        <el-form-item label="MMSI号" prop="MMSI">
          <el-input v-model="form.MMSI" placeholder="请输入MMSI号" />
        </el-form-item>
        <el-form-item label="注册港口代码" prop="regportCode">
          <el-input v-model="form.regportCode" placeholder="请输入注册港口代码" />
        </el-form-item>
        <el-form-item label="类型代码" prop="shipTypeCode">
          <el-input v-model="form.shipTypeCode" placeholder="请输入类型代码" />
        </el-form-item>
        <el-form-item label="总吨位" prop="shipGrossTon">
          <el-input v-model="form.shipGrossTon" placeholder="请输入总吨位" />
        </el-form-item>
        <el-form-item label="净吨位" prop="shipNetTon">
          <el-input v-model="form.shipNetTon" placeholder="请输入净吨位" />
        </el-form-item>
        <el-form-item label="载重吨" prop="shipDwt">
          <el-input v-model="form.shipDwt" placeholder="请输入载重吨" />
        </el-form-item>
        <el-form-item label="长度" prop="shipLength">
          <el-input v-model="form.shipLength" placeholder="请输入长度" />
        </el-form-item>
        <el-form-item label="宽度" prop="shipBreadth">
          <el-input v-model="form.shipBreadth" placeholder="请输入宽度" />
        </el-form-item>
        <el-form-item label="深度" prop="shipDepth">
          <el-input v-model="form.shipDepth" placeholder="请输入深度" />
        </el-form-item>
        <el-form-item label="航线代码" prop="lineCode">
          <el-input v-model="form.lineCode" placeholder="请输入航线代码" />
        </el-form-item>
        <el-form-item label="区域代码" prop="areaCode">
          <el-input v-model="form.areaCode" placeholder="请输入区域代码" />
        </el-form-item>
        <el-form-item label="建造日期" prop="constructionDate">
          <el-date-picker clearable
            v-model="form.constructionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择建造日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShips, getShips, delShips, addShips, updateShips } from "@/api/dataSys/ships";

export default {
  name: "Ships",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 船舶信息管理表格数据
      shipsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shipId: null,
        shipRegNo: null,
        shipFirstRegNo: null,
        shipInspectNo: null,
        MMSI: null,
        regportCode: null,
        shipRegionType: null,
        shipTypeCode: null,
        shipGrossTon: null,
        shipNetTon: null,
        shipDwt: null,
        shipLength: null,
        shipBreadth: null,
        shipDepth: null,
        lineCode: null,
        areaCode: null,
        constructionDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询船舶信息管理列表 */
    getList() {
      this.loading = true;
      listShips(this.queryParams).then(response => {
        this.shipsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        shipId: null,
        shipRegNo: null,
        shipFirstRegNo: null,
        shipInspectNo: null,
        shipNameCn: null,
        MMSI: null,
        regportCode: null,
        shipRegionType: null,
        shipTypeCode: null,
        shipGrossTon: null,
        shipNetTon: null,
        shipDwt: null,
        shipLength: null,
        shipBreadth: null,
        shipDepth: null,
        lineCode: null,
        areaCode: null,
        constructionDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.shipNameCn)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加船舶信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const shipNameCn = row.shipNameCn || this.ids
      getShips(shipNameCn).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改船舶信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.shipNameCn != null) {
            updateShips(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShips(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const shipNameCns = row.shipNameCn || this.ids;
      this.$modal.confirm('是否确认删除船舶信息管理编号为"' + shipNameCns + '"的数据项？').then(function() {
        return delShips(shipNameCns);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dataSys/ships/export', {
        ...this.queryParams
      }, `ships_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
